package tags.templates;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import main.Basket;
import main.Item;
import main.PMF;

import DBClasses.Game;
import DBClasses.Game.PriceTypes;
import DBClasses.GameType;

public class BasketTag extends TagSupport
{
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException
	{
		String result = "";

		Basket bskt = (Basket) ((HttpServletRequest) pageContext.getRequest())
				.getSession().getAttribute("basket");
		if (bskt == null || bskt.UserBasket.size() <= 0)
		{
			((HttpServletRequest) pageContext.getRequest()).getSession()
					.setAttribute("basket", new Basket());
			result = "<div style=\"margin:20px\">Игр в корзине пока нет...</div>";
		} else
		{
			PersistenceManager manager = PMF.get().getPersistenceManager();
			result = "<table>";
			if (bskt.UserBasket.size() > 0)
				result += "<tr>"
						+ "<td class = \"caption\">Название</td><td class = \"caption\">Категория</td><td class = \"caption\">Цена</td><td class = \"caption\">Количество</td>";
			for (Item item : bskt.UserBasket)
			{
				Game game = item.getGame();
				String cat = "0";
				List<GameType> gts = (List<GameType>) manager
						.newQuery(
								"select from " + GameType.class.getName()
										+ " where name == '"
										+ game.getGameType() + "'").execute();
				if (gts != null && gts.size() > 0)
					cat = gts.get(0).getId().toString();
				String game_link = "0";
				List<Game> gs = (List<Game>) manager.newQuery(
						"select from " + Game.class.getName() + " where id == "
								+ game.getId()).execute();
				if (gs != null && gs.size() > 0)
					game_link = gs.get(0).getId().toString();
				result += "<tr>"
						+ "<td><a class = \"no_underline\" href=\"/items.jsp?id="
						+ game_link
						+ "\">"
						+ game.getName()
						+ "</a></td>"
						+ "<td><a class = \"no_underline\" href=\"/categories.jsp?id="
						+ cat
						+ "\">"
						+ game.getGameType()
						+ "</a></td>"
						+ "<td>"
						+ game.getPrice(PriceTypes.Rubl).toString()
						+ "</td>"
						+ "<td><input id=\"cnt_"+ item.getGame().getId()+"\" type=\"text\" onkeypress=\"ChangeItem("
						+ item.getGame().getId()
						+ ");\" value=\""
						+ item.Count
						+ "\"/></td><td><input type=\"submit\" value=\"Удалить\" onclick=\"DeleteBasketItem("
						+ +item.getGame().getId() + ");\"></td></tr>";
			}
			manager.close();
			result += "</table><form id=pay name=pay method=\"POST\" action=\"https://merchant.webmoney.ru/lmi/payment.asp\">"
					+ "<p>"
					+ "<input type=\"hidden\" name=\"LMI_PAYMENT_AMOUNT\" id=\"total_price\" value=\"1.0\">"
					+ "<input type=\"hidden\" name=\"LMI_PAYMENT_DESC\" value=\"test\">"
					+ "<input type=\"hidden\" name=\"LMI_PAYMENT_NO\" value=\"1\">"
					+ "<input type=\"hidden\" name=\"LMI_PAYEE_PURSE\" value=\"R401857973278\">"
					+ "<input type=\"hidden\" name=\"LMI_SIM_MODE\" value=\"0\">"
					+ "</p>"
					+ "<input type=\"submit\" id=\"Buy\" class=\"log_reg_btn top_text_stl\" onclick=\"BuyAllInBasket();\" value=\"Оплатить\"/>"
					+ "</form>";
		}
		try
		{
			pageContext.getOut().flush();
			pageContext.getOut().print(result);
		} catch (Exception ex)
		{
			throw new JspException(ex.getMessage());
		}
		return SKIP_BODY;
	}

	public void release()
	{
	}
}
